package com.nativetalk.service.live.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.live.*;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/5/14.
 */
@Service
public class LiveSearchServiceImpl implements LiveSearchService {
    private static Logger logger = Logger.getLogger(LiveSearchServiceImpl.class);
    @Resource
    private TdNtLiveCourseDao tdNtLiveCourseDao;
    @Resource
    private TdNtLivePictureDao tdNtLivePictureDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtLiveStudyDao tdNtLiveStudyDao;
    @Override
    public RetInfo findLivePictures(int live_cou_id) {
        String logInfo = this.getClass().getName() + ":findLivePictures:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            List<TdNtLivePictureList> tdNtLivePictureLists = tdNtLivePictureDao.findTdNtLivePictureByLiveId(live_cou_id);
            if (tdNtLivePictureLists.size()>0) {
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取图片成功");
                retInfo.setObj(tdNtLivePictureLists);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("没有图片");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveByState(int tea_id, Page page) {
        String logInfo = this.getClass().getName() + ":findLiveByState:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("shipin", "/waijiaojun/app/teacher-helper-live/live.mp4");
            Map<String,Object> map = new HashMap<>();
            map.put("tea_id", tea_id);
            map.put("state", page.getObj());
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseDao.findTdNtLiveCourseListSize(map);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }

                map.put("currentPage",(page.getCurrentPage()-1)*pageSize);
                map.put("pageSize",pageSize);
                List<TdNtLiveCourseList> tdNtLiveCourseLists = tdNtLiveCourseDao.findTdNtLiveCourseList(map);

                result.put("lives", tdNtLiveCourseLists);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播课程成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播课程");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLives(Page page) {
        String logInfo = this.getClass().getName() + ":findLives:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisListSize();//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
                List<TdNtLiveCourseHisList> tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisList(page);

                for (int i=0 ; i < tdNtLiveCourseHisLists.size() ; i ++ ) {
                    TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tdNtLiveCourseHisLists.get(i).getTea_id());
                    tdNtLiveCourseHisLists.get(i).setTea_head_portrait(tdNtTeacherIndex.getTea_head_portrait());
                    tdNtLiveCourseHisLists.get(i).setTea_name(tdNtTeacherIndex.getTea_name());
                }
                Map<String, Object> result = new HashMap<>();
                result.put("lives", tdNtLiveCourseHisLists);
                result.put("totalPage", totalPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播列表成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无直播");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveDetail(int live_cou_his_id, TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":findLiveDetail:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            /**
             * 查找直播详情(id,封面,直播名称,时长,费用,简介,教师头像,名称,评分,频道)
             */
            TdNtLiveCourseHisDetails tdNtLiveCourseHisDetails = tdNtLiveCourseHisDao.findTdNtLiveCourseHisDetails(live_cou_his_id);
            if (tdNtLiveCourseHisDetails != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("live", tdNtLiveCourseHisDetails);
                /**
                 * 已购买直播人数,直播人头像
                 */
                List<TdNtLiveHisMemberHead> liveHisMemberHeads = tdNtLiveStudyDao.findTdNtLiveHisMemberHeads(live_cou_his_id);
                map.put("memb_head", liveHisMemberHeads);
                /**
                 * 是否购买改直播
                 */
                map.put("is_but", 1);
                if (tdNtMember != null ){
                    Map<String, Integer> params = new HashMap<>();
                    params.put("live_cou_his_id", live_cou_his_id);
                    params.put("memb_id", tdNtMember.getMemb_id());
                    TdNtLiveStudyDetail tdNtLiveStudyDetail = tdNtLiveStudyDao.findTdNtLiveStudyDetail(params);
                    if (tdNtLiveStudyDetail != null) {
                        map.put("is_but", 0);
                    }
                }
                List<TdNtLivePictureList> tdNtLivePictureLists = tdNtLivePictureDao.findTdNtLivePictureByLiveId(tdNtLiveCourseHisDetails.getLive_cou_id());
                map.put("pictures", tdNtLivePictureLists);
                map.put("share",0);
                map.put("share_name","大开眼界，寓教于乐，体验世界风情和新奇！我正在看外教君直播！");
                map.put("share_url", Constants.share_live_url + live_cou_his_id);
                map.put("share_picutre",tdNtLiveCourseHisDetails.getLive_cou_his_picture());
                map.put("share_content",tdNtLiveCourseHisDetails.getTea_name() + "向你发起了一个英语对话，赶紧点开回复吧！");
                String is_collection = "1";
                if (tdNtMember != null) {
                    Map<String, Object> obj = new HashMap<String, Object>();
                    obj.put("tea_id", tdNtLiveCourseHisDetails.getTea_id());
                    obj.put("memb_id", tdNtMember.getMemb_id());
                    //给查询到的老师教师信息添加收藏字段
                    String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                    if (flag != null) {
                       is_collection = flag;
                    }
                }
                map.put("is_collection",is_collection);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播数据成功");
                retInfo.setObj(map);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("直播不存在");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findLiveHisByTeaId( Page page) {
        String logInfo = this.getClass().getName() + ":findLiveHisByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            result.put("shipin","/resource/waijiaojun/app/live/live.mp4");
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisTeacherListSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveCourseHisTeacherList> tdNtLiveCourseHisTeacherLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisTeacherList(page);

                result.put("lives", tdNtLiveCourseHisTeacherLists);
                result.put("totalPage", totalPage);


                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findMemberLives(Page page) {
        String logInfo = this.getClass().getName() + ":findMemberLives:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCourseHisDao.findTdNtLiveCourseHisListByMembIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
                List<TdNtLiveCourseHisList> tdNtLiveCourseHisLists = tdNtLiveCourseHisDao.findTdNtLiveCourseHisByMembIdList(page);

                for (int i=0 ; i < tdNtLiveCourseHisLists.size() ; i ++ ) {
                    TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tdNtLiveCourseHisLists.get(i).getTea_id());
                    tdNtLiveCourseHisLists.get(i).setTea_head_portrait(tdNtTeacherIndex.getTea_head_portrait());
                    tdNtLiveCourseHisLists.get(i).setTea_name(tdNtTeacherIndex.getTea_name());
                }
                result.put("lives", tdNtLiveCourseHisLists);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播列表成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public TdNtLiveCourseHisDetails findTdNtLiveCourseHisDetails(int live_cou_his_id) {
        return tdNtLiveCourseHisDao.findTdNtLiveCourseHisDetails(live_cou_his_id);
    }

    @Override
    public List<TdNtLiveHisMemberHead> findTdNtLiveHisMemberHead(int live_cou_his_id) {
        return tdNtLiveStudyDao.findTdNtLiveHisMemberHeads(live_cou_his_id);
    }

    @Override
    public List<TdNtLiveCourseHisList> findTdNtLiveCourseHisByMembIdListIndex() {

        return tdNtLiveCourseHisDao.findTdNtLiveCourseHisByMembIdListIndex();
    }


}
